package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsNarratorDetail;
import com.macro.mall.dto.BmsNarratorParam;
import com.macro.mall.dto.BmsNarratorDetail;
import com.macro.mall.mapper.BmsMuseumMapper;
import com.macro.mall.mapper.BmsNarratorMapper;
import com.macro.mall.model.*;
import com.macro.mall.model.BmsNarrator;
import com.macro.mall.model.BmsNarratorExample;
import com.macro.mall.service.BmsNarratorPicService;
import com.macro.mall.service.BmsNarratorService;
import com.macro.mall.service.BmsNarratorService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class BmsNarratorServiceImpl implements BmsNarratorService {

    @Autowired
    private BmsNarratorMapper bmsNarratorMapper;

    @Resource
    private BmsMuseumMapper bmsMuseumMapper;

    @Autowired
    private BmsNarratorPicService bmsNarratorPicService;

    @Autowired
    private UserUtils userUtils;


    @Override
    public List<BmsNarrator> getList(BmsNarratorParam bmsNarratorParam) {
        PageHelper.startPage(bmsNarratorParam.getPageNum(), bmsNarratorParam.getPageSize());
        BmsNarratorExample example = new BmsNarratorExample();
        BmsNarratorExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        if (StringUtils.isNotBlank(bmsNarratorParam.getName())) {
            criteria.andNameLike("%" + bmsNarratorParam.getName() + "%");
        }
        example.setOrderByClause("create_time desc");

        List<BmsMuseum> allList = bmsMuseumMapper.findAllList();

        List<BmsNarrator> list = bmsNarratorMapper.selectByExample(example);
        Map<Long, BmsMuseum> museumMap = allList.stream().collect(Collectors.toMap(BmsMuseum::getId, Function.identity()));
        for (BmsNarrator bmsNarrator : list) {
            BmsMuseum bmsMuseum = museumMap.getOrDefault(bmsNarrator.getMuseumId(), new BmsMuseum());
            if (ObjectUtils.isNotEmpty(bmsMuseum.getName())) {
                bmsNarrator.setMuseumName(bmsMuseum.getName());
            }
        }

        return list;
    }

    @Override
    public int create(BmsNarratorParam bmsNarratorParam) {
        BmsNarrator bmsTourismProject = new BmsNarrator();
        BeanUtils.copyProperties(bmsNarratorParam, bmsTourismProject);
        Long userId = userUtils.getUserId();
        bmsTourismProject.setCreateUser(userId);
        bmsTourismProject.setUpdateUser(userId);
        bmsTourismProject.setCreateTime(new DateTime());
        bmsTourismProject.setUpdateTime(new DateTime());
        int count = bmsNarratorMapper.insertSelective(bmsTourismProject);

        String coverPic = bmsNarratorParam.getCoverPic();
        List<String> picList = bmsNarratorParam.getPicList();
        bmsNarratorPicService.create(bmsTourismProject.getId(), coverPic, picList);

        return count;
    }

    @Override
    public int update(BmsNarratorParam bmsNarratorParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsNarrator bmsTourismProject = new BmsNarrator();
        bmsTourismProject.setId(bmsNarratorParam.getId());
        BeanUtils.copyProperties(bmsNarratorParam, bmsTourismProject);
        bmsTourismProject.setUpdateTime(new DateTime());
        bmsTourismProject.setUpdateUser(userId);
        int conut = bmsNarratorMapper.updateByPrimaryKeySelective(bmsTourismProject);

        bmsNarratorPicService.deleteByActivitiesId(bmsNarratorParam.getId());

        String coverPic = bmsNarratorParam.getCoverPic();
        List<String> picList = bmsNarratorParam.getPicList();
        bmsNarratorPicService.create(bmsNarratorParam.getId(), coverPic, picList);

        return conut;
    }

    @Override
    public BmsNarratorDetail getItem(Long id) {
        BmsNarrator bmsNarrator = bmsNarratorMapper.selectByPrimaryKey(id);
        BmsNarratorDetail detail = new BmsNarratorDetail();
        BeanUtils.copyProperties(bmsNarrator, detail);
        // 获取封面图
        String coverPic = bmsNarratorPicService.findCoverPicByActivitiesId(bmsNarrator.getId());
        // 获取活动图片
        List<String> picList = bmsNarratorPicService.findPicListByActivitiesId(bmsNarrator.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Override
    public int delete(List<Long> ids) {
        BmsNarratorExample example = new BmsNarratorExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        BmsNarrator updateDetail = new BmsNarrator();
        updateDetail.setUpdateUser(userId);
        updateDetail.setUpdateTime(new Date());
        updateDetail.setIsDelete((byte) 1);
        return bmsNarratorMapper.updateByExampleSelective(updateDetail,example);
    }
}
